package com.hevery.blog.mail;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.apachecommons.CommonsLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * @description: 邮件发送工具类
 * @author: hongyu.zhang
 * @create: 2018-03-30
 **/

@Slf4j
@Component
@ConfigurationProperties(prefix = "mail")
public class MailUtil {

    @Autowired
    private JavaMailSender sender;

    /**
     * 发送人
     */
    @Getter @Setter
    private String from;

    /**
     * 发送文本邮件
     * @param to 接收人
     * @param subject 主题
     * @param text 文本
     */
    public void sendText(String to, String subject, String text) throws MessagingException{
        log.info("===>to:{}, subject:{}, text:{}",  to, subject, text);
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);
        sender.send(message);
        log.info("===>send text mail finish");
    }

    /**
     * 发送html邮件
     * @param to 接收人
     * @param subject 主题
     * @param text html文本
     * @throws MessagingException
     */
    public void sendHtml(String to, String subject, String text) throws  MessagingException{
        log.info("===>to:{}, subject:{}, text:{}",  to, subject, text);
        MimeMessage message = sender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(text,true);
        sender.send(message);
        log.info("===>send html mail finish");
    }

    /**
     * 发送附件邮件
     * @param to 接收人
     * @param subject 主题
     * @param text text
     * @param filePath 附件路径
     * @throws MessagingException
     */
    public void sendAttachments(String to, String subject, String text, String filePath) throws MessagingException{
        log.info("===>to:{}, subject:{}, text:{}, resPath:{}, resId:{}", to, subject, text, filePath);
        MimeMessage message = sender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message,true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(text);
        FileSystemResource file = new FileSystemResource(new File(filePath));
        String attachmentFilename = filePath.substring(filePath.lastIndexOf(File.separator) + 1);
        helper.addAttachment(attachmentFilename,file);
        sender.send(message);
        log.info("===>send attachments mail finish");
    }

    /**
     * 发送嵌入资源(一般是图片)邮件
     * @param to 接收人
     * @param subject 主题
     * @param text 带图片的邮件<img src=\"cid：resId\"/>
     * @param resPath
     * @param resId
     */
    public void sendInlineResource(String to , String subject, String text,String resPath, String resId) throws MessagingException{
        log.info("===>to:{}, subject:{}, text:{}, resPath:{}, resId:{}", to, subject, text, resPath, resId);
        MimeMessage message = sender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message,true);
        helper.setTo(to);
        helper.setFrom(from);
        helper.setSubject(subject);
        helper.setText(text,true);
        FileSystemResource resource = new FileSystemResource(new File(resPath));
        helper.addInline(resId,resource);
        sender.send(message);
        log.info("===>send inline resource mail finish");
    }
}
